package com.zhang.zhangapp.view.mine;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zhang.zhangapp.LoginActivity;
import com.zhang.zhangapp.R;
import com.zhang.zhangapp.application.MApplication;
import com.zhang.zhangapp.base.Constant;
import com.zhang.zhangapp.base.config.BaseConfig;
import com.zhang.zhangapp.base.http.MCallback;
import com.zhang.zhangapp.bean.TokenData;
import com.zhang.zhangapp.http.CommonHttp;
import com.zhang.zhangapp.utils.ActivityManager;
import com.zhang.zhangapp.utils.CreateMD5;
import com.zhang.zhangapp.utils.T;
import com.zhang.zhangapp.view.BaseAppCompatActivityActivity;

import org.xutils.common.Callback;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.HashMap;
import java.util.Map;

@ContentView(R.layout.activity_set_mobail)
public class ThirdLoginSetMobail extends BaseAppCompatActivityActivity {
    @ViewInject(R.id.setting_code_text)
    EditText codeText;
    @ViewInject(R.id.setting_phone_text)
    EditText phoneText;
    @ViewInject(R.id.get_code)
    TextView getCodeText;
    @ViewInject(R.id.action_bar_title)
    TextView titleText;
    private String thirdNum;
    private String type;
    MCallback getCodeCallBack = new MCallback() {
        @Override
        public void onSuccess(String data) {
            time.start();
        }

        @Override
        public void onSuccess(String message, String code) {
        }

        @Override
        public void onError(Throwable ex, boolean isOnCallback) {
        }

        @Override
        public void onCancelled(Callback.CancelledException cex) {

        }

        @Override
        public void onFinished() {

        }
    };

    private TimeCount time;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        thirdNum = getIntent().getStringExtra("thirdNum");
        type = getIntent().getStringExtra("type");
        x.view().inject(this);
        titleText.setText("绑定手机");
        time = new TimeCount(60000, 1000);
    }

    /*
  获取短信验证码
   */
    @Event(type = View.OnClickListener.class, value = R.id.get_code)
    private void getCode(View view) {
        try {
            if (phoneText.getText().toString().equals("")) {
                T.showShort(this, "请填写手机号码");
                return;
            }
            Map<String, String> params = new HashMap<String, String>();
            params.put("mobileNum", phoneText.getText().toString());
            params.put("type", Constant.绑定手机);
            params.put("secretKey", CreateMD5.getMD5(phoneText.getText().toString() + Constant.绑定手机 + BaseConfig.SECRETKEY));
            CommonHttp.commonHttp("hzg/sms/sendCode", params, getCodeCallBack);
        }catch (Exception e){
            T.showShort(this, e.getMessage());
        }

    }

    @Event(value = R.id.setting_sure)
    private void bindPhone(View view) {
        if (phoneText.getText().toString().equals("")) {
            T.showShort(this, "请填写手机号码");
            return;
        }
        if (codeText.getText().toString().equals("")) {
            T.showShort(this, "请填写验证码");
            return;
        }
        if (showLoadingDialog()) {
            Map<String, String> params = new HashMap<String, String>();
            params.put("mobileNum", phoneText.getText().toString());
            params.put("thirdNum", thirdNum);
            params.put("type", type);
            params.put("smsCode", codeText.getText().toString());
            CommonHttp.commonHttp("hzg/login/bandMobile", params, new MCallback() {
                @Override
                public void onSuccess(String data) {
                    try {
                        Gson gson = new Gson();
                        TokenData tokenData = gson.fromJson(data, TokenData.class);
                        if (tokenData != null) {
                            MApplication.getInstance().setUser(tokenData.getMember(), tokenData.getTokens(), tokenData.getTickets());
                            setResult(RESULT_OK);
                            ThirdLoginSetMobail.this.finish();
                        }
                    } catch (Exception e) {
                        T.showShort(ThirdLoginSetMobail.this, "请求失败");
                    }
                }

                @Override
                public void onSuccess(String message, String code) {

                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {

                }

                @Override
                public void onCancelled(Callback.CancelledException cex) {

                }

                @Override
                public void onFinished() {
                    dismisionDalog();
                }
            });
        }
    }

    class TimeCount extends CountDownTimer {
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);// 参数依次为总时长,和计时的时间间隔
        }

        @Override
        public void onFinish() {// 计时完毕时触发
            getCodeText.setText("获取验证码");
            getCodeText.setEnabled(true);
        }

        @Override
        public void onTick(long millisUntilFinished) {// 计时过程显示
            getCodeText.setEnabled(false);
            getCodeText.setText(millisUntilFinished / 1000 + "秒");
        }
    }
}
